/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import business.UserManager;
import entity.User;
import exceptions.DuplicateUserException;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 * This bean handles the registration of an user
 *
 * @author Alessandro
 */
@Named(value = "registrationBean")
@RequestScoped
public class RegistrationBean {

    private User user;
    @EJB
    UserManager um;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public RegistrationBean() {
        um = new UserManager();
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String register() {
       try{
           um.save(user);
            return "index";
       }catch (DuplicateUserException e){
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Email already used."));
            return "registration";
        }

    }

}
